package shuiguo;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import org.junit.jupiter.api.Test;

import shuiguo.bean.JinHuoDanBean;
import shuiguo.bean.ShuiGuoBean;
import shuiguo.bean.XiaoShouBean;
import shuiguo.dao.JinHuoDanDao;
import shuiguo.dao.ShuiGuoDao;
import shuiguo.dao.XiaoShouDao;

public class ShuiGuo {
	// 进货单
	private static List<JinHuoDanBean> jinHuoDanList = new ArrayList<JinHuoDanBean>();
	// 库存
	private static List<ShuiGuoBean> shuiGuoList = new ArrayList<ShuiGuoBean>();
	// 销售记录
	private static List<XiaoShouBean> xiaoShouList = new ArrayList<XiaoShouBean>();

	public static void main(String[] args) {
		while (true) {
			System.out.println("1.添加进货单");
			System.out.println("2.显示所有进货单");
			System.out.println("3.显示当前各种水果的库存数，单价，销售数量，销售金额，进货数量，进货金额");
			System.out.println("4.加入其他水果品种");
			System.out.println("5.顾客购物");
			System.out.println("6.查询全部销售记录信息");
			System.out.println("7.按销售数量排序显示前三名");
			ShuiGuo s = new ShuiGuo();
			s.caozuo();
		}
	}

	public void caozuo() {
		Scanner sc = new Scanner(System.in);
		String xuanze = sc.nextLine();
		switch (xuanze) {
		case "1":
			tianJiaJinHuo();
			break;
		case "2":
			xianShiJinHuo();
			break;
		case "3":
			showKuCun();
			break;
		case "4":
			tianJiaOther();
			break;
		case "5":
			xiaoFei();
			break;
		case "6":
			showXiaoFei();
			break;

		case "7":
			showByXiaoShouNumber();
			break;

		default:
			break;
		}
	}

	@Test
	public void xianShiJinHuo() {
		for (int i = 0; i < jinHuoDanList.size(); i++) {
			JinHuoDanBean jinHuoDanBean = jinHuoDanList.get(i);
			System.out.println(jinHuoDanBean.toString());
		}
	}

	@Test
	public void tianJiaJinHuo() {
		JinHuoDanBean jinhuo = new JinHuoDanBean();
		ShuiGuoBean kucun = new ShuiGuoBean();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入进货水果种类：");
		String shuiGuoName = sc.nextLine();
		System.out.println("请输入水果单价：");
		BigDecimal xiaoShouDanJia = new BigDecimal(sc.nextLine());

		System.out.println("请输入进货数量：");
		String num = sc.nextLine();
		int shuiGuoNum = Integer.parseInt(num);
		BigDecimal jiSuanNum = new BigDecimal(num);

		System.out.println("请输入进货单价：");
		BigDecimal JinHuoDanJia = new BigDecimal(sc.nextLine());

		// 进货单dao
		JinHuoDanDao jinhuoDao = new JinHuoDanDao();
		// 水果dao
		ShuiGuoDao shuiguoDao = new ShuiGuoDao();

		// 根据水果种类获取该水果bean
		ShuiGuoBean shui = new ShuiGuoBean();
		shui.setShuiGuoName(shuiGuoName);
		ShuiGuoBean shui1 = shuiguoDao.select(shui);
//		System.out.println(shui1);
		// 如果本来就有是更新，没有是插入
		if (shui1 != null) {
			// 新的对象还没给水果种类赋值
			ShuiGuoBean shuiGeng = new ShuiGuoBean();

			// 坑坑坑，给水果种类
			shuiGeng.setShuiGuoName(shuiGuoName);
			shuiGeng.setNumber(shui1.getNumber() + shuiGuoNum);
			shuiGeng.setJinHuoMoney(shui1.getJinHuoMoney().add(jiSuanNum.multiply(JinHuoDanJia)));
			shuiGeng.setJinHuoNum(shuiGuoNum + shui1.getJinHuoNum());
			// 更新已有水果库存
			int i = shuiguoDao.update(shuiGeng);
			if (i!=0) {
				System.out.println(shuiGuoName+"进货成功");
			}
		} else {
			// 添加库存
			kucun.setJinHuoNum(shuiGuoNum);
			kucun.setShuiGuoName(shuiGuoName);
			kucun.setJinHuoMoney(jiSuanNum.multiply(JinHuoDanJia));
			kucun.setDanJia(xiaoShouDanJia);
			kucun.setNumber(shuiGuoNum);
			int i=shuiguoDao.insert(kucun);
			if (i!=0) {
				System.out.println(shuiGuoName+"进货成功");
			}
		}
		// 添加进货单
		jinhuo.setShuiGuoName(shuiGuoName);
		jinhuo.setDanJia(xiaoShouDanJia);
		jinhuo.setNumber(shuiGuoNum);
		jinhuo.setJinHuoMoney(jiSuanNum.multiply(JinHuoDanJia));
		jinhuo.setJinHuodanJia(JinHuoDanJia);
		jinhuoDao.insert(jinhuo);
	}
@Test
	public void showKuCun() {
	System.out.println("显示各种水果的信息");
		ShuiGuoDao dao=new ShuiGuoDao();
		List<ShuiGuoBean> list = dao.selectAll();
		for (int i = 0; i < list.size(); i++) {
			ShuiGuoBean shuiGuoBean = list.get(i);
			System.out.println(shuiGuoBean.toString());
		}
	}

	/**
	 * 添加其他水果品种，在库存list中添加，如果存在就结束
	 */
	@Test
	public void tianJiaOther() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入要添加的水果品种");
		String leixing = sc.nextLine();

		ShuiGuoBean shuiGuoBean = new ShuiGuoBean();
		shuiGuoBean.setShuiGuoName(leixing);
		ShuiGuoDao dao = new ShuiGuoDao();
		ShuiGuoBean shui = dao.select(shuiGuoBean);
		int i = 0;
		if (shui != null) {

			System.out.println("该水果类型已经存在");
			return;
		} else {
			i = dao.insertLei(shuiGuoBean);
		}

		if (i == 1) {

			System.out.println("新品种添加成功");
		} else {
			System.out.println("新品种添加失败");
		}
	}

	/**
	 * 根据时间17:00前后，和顾客性别确定水果单价
	 */
	@Test
	public BigDecimal feiYong(int sex, String shuiGuo) {
		Date time = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		int hour1 = cal.get(Calendar.HOUR_OF_DAY);

		ShuiGuoBean bean = new ShuiGuoBean();
		bean.setShuiGuoName(shuiGuo);
		ShuiGuoDao shuidao = new ShuiGuoDao();
		// 找出该水果品种信息
		ShuiGuoBean shui1 = shuidao.select(bean);

		BigDecimal danjia = new BigDecimal(0);
		int status = 0;
		ShuiGuoBean shui = null;
		if (shui1 != null) {
			danjia = shui1.getDanJia();
			status = 1;
			if (status == 0) {
				System.out.println("没有该品种");
				return null;
			}

			if (hour1 >= 17) {
				danjia = danjia.multiply(new BigDecimal(0.5));
			}
			if (sex == 1) {
				danjia = danjia.multiply(new BigDecimal(0.8));
			}

		}
		return danjia;
	}

	/**
	 * 判断输入的数据在链表中是否存在
	 * 
	 * @param <T>
	 */
//	public <ShuiGuoBean> void isExist(ArrayList<ShuiGuoBean> array,String shuru) {
//		for (int i = 0; i < array.size(); i++) {
//			ShuiGuoBean t = array.get(i);
//			
//		}
//	}
	@Test
	public void xiaoFei() {
		Scanner sc = new Scanner(System.in);
		System.out.println("顾客性别");
		String xingbie = sc.nextLine();
		if (!"女".equals(xingbie) && !"男".equals(xingbie)) {
			System.out.println("请输入男或女");
			return;
		}
		int sex = 0;
		if ("女".equals(xingbie)) {
			sex = 1;
		}
		System.out.println("买什么水果");
		String shuiGuo = sc.nextLine();
		System.out.println("购买数量");
		String s=sc.nextLine();
		Integer number = Integer.valueOf(s);
		BigDecimal suanNumber = new BigDecimal(s);
		// 修改货存list中的水果数量、销售数量、销售金额，增加销售记录水果名称、顾客性别、数量、单价、销售金额
		ShuiGuoDao shuiguodao = new ShuiGuoDao();
		ShuiGuoBean shui = new ShuiGuoBean();
		shui.setShuiGuoName(shuiGuo);
		ShuiGuoBean shui1 = shuiguodao.select(shui);
		if (shui1 != null) {
			System.out.println("水果种类存在");
		} else {
			System.out.println("水果种类不存在！");
			return;
		}

		if (shui1.getNumber() >= number) {
			System.out.println("库存充足");
		} else {
			System.out.println("库存不足");
			return;
		}

		Date cal = Calendar.getInstance().getTime();
		SimpleDateFormat dmt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String xiaoshouTime = dmt.format(cal);

		// 水果bean存修改的数据
		ShuiGuoBean shui3 = new ShuiGuoBean();
		// 销售记录bean存修改的数据
		XiaoShouBean xiaoshou = new XiaoShouBean();

		// money转换成BigDecimal类
		BigDecimal money = feiYong(sex, shuiGuo);

		shui3.setShuiGuoName(shuiGuo);
		shui3.setNumber(shui1.getNumber() - number);
		shui3.setXiaoShouNum(shui1.getXiaoShouNum() + number);
		shui3.setXiaoShouMoney(shui1.getXiaoShouMoney().add(suanNumber.multiply(money)));
		int i = shuiguodao.updateXiao(shui3);
		System.out.println("修改" + i + "条水果记录");

		xiaoshou.setXiaoShouTime(xiaoshouTime);
		xiaoshou.setDanJia(money);
		xiaoshou.setNumber(number);
		xiaoshou.setSex(sex);
		xiaoshou.setShuiGuoName(shuiGuo);
		xiaoshou.setXiaoShouMoney(suanNumber.multiply(money));
		XiaoShouDao xiaodao = new XiaoShouDao();
		int j = xiaodao.insert(xiaoshou);
		System.out.println("销售记录更新" + j + "条记录");
	}

	@Test
	public void showXiaoFei() {
		System.out.println("全部消费记录：");
		XiaoShouDao xiaodao=new XiaoShouDao();
		List<XiaoShouBean> list = xiaodao.selectAll();
		for (int i = 0; i < list.size(); i++) {
			XiaoShouBean xiaoShouBean = list.get(i);

			System.out.println(xiaoShouBean.toString());
		}
	}

	@Test
	public void showByXiaoShouNumber() {

		ShuiGuoDao shuidao=new ShuiGuoDao();
		List<ShuiGuoBean> list = shuidao.selectAll();
		java.util.Collections.sort(list, new Comparator<ShuiGuoBean>() {
			@Override
			public int compare(ShuiGuoBean o1, ShuiGuoBean o2) {
				return o2.getXiaoShouNum() - o1.getXiaoShouNum();
			}
		});
		System.out.println("按销售数量排名前三：");
		for (int i = 0; i < 3; i++) {

			ShuiGuoBean ShuiGuoBean = list.get(i);
			System.out.println(ShuiGuoBean.toString());

		}

	}
}
